package com.qf.program.service.yjpy;

import cn.hutool.core.collection.ListUtil;
import com.qf.common.utils.CheckUtil;
import com.qf.common.utils.RedisUtil;
import com.qf.utils.SmallTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * @Author: gcr
 * @Date: 2022-1-17 13:34
 */

@Service
@Slf4j
public class GetSubJectScoreService {


    //2022年2月10日13:42:21 获取更多数据进行
    public Object getMoreData(String rekey, String type, Integer page) {
        Object object = RedisUtil.getObject(rekey);
        Map<String, Object> resFinal = new LinkedHashMap<>();

        if (object == null) {
            CheckUtil.check(false, "未获取到数据,请重试");
        }
        SmallTool.printMess("执行查询数据");
        Map<String, List<Res>> res = (Map<String, List<Res>>) object;
        List<Res> o = res.get(type);
        List<String> collect = Stream.of(QueryField.PAGEchinaHosp, QueryField.PAGEotherHosp, QueryField.PAGEchinaUniv, QueryField.PAGEotherUniv).collect(Collectors.toList());
        if (collect.contains(type)) {
            List<List<Res>> split = ListUtil.split(o, 20);
            resFinal.put("allpage", split.size());
            resFinal.put("data", split.get(page - 1));
            return resFinal;
        }
        return o;
    }


    public Object getMoreDataDetail(String rekey, String type, String key, Integer page) {
        Object object = RedisUtil.getObject(rekey);
        Map<String, Object> resFinal = new LinkedHashMap<>();
        if (object == null) {
            CheckUtil.check(false, "未获取到数据,请重试");
        }
        Map<String, List<Res>> res = (Map<String, List<Res>>) object;
        List<Res> o = res.get(type);
        //ramData.put("chinaUniv", (unitLs));
        // ramData.put("chinaHosp", unitLChinaHospLs);
        // ramData.put("otherUniv", unitLOtherUnivLs);
        // ramData.put("otherHosp", unitLOtherHospLs);

        List<String> collect = Stream.of(QueryField.PAGEchinaHosp, QueryField.PAGEotherHosp, QueryField.PAGEchinaUniv, QueryField.PAGEotherUniv).collect(Collectors.toList());
        if (collect.contains(type)) {
            List<List<Res>> split = ListUtil.split(o, 20);
            resFinal.put("allpage", split.size());
            resFinal.put("data", split.get(page - 1));
        } else {
            Optional<Res> first = o.stream().filter(sin -> sin.getFrontKey().equals(key)).findFirst();
            if (first.isPresent()) {
                Res res1 = first.get();
                return res1;
            } else {
                CheckUtil.check(false, "点击错误不存在");
            }
        }


        return o;
    }


}
